package com.liujit.bms.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.app.domain.model.BlogMemberFeedback;
import com.liujit.bms.app.domain.params.BlogMemberFeedbackParams;
import com.liujit.bms.app.domain.query.BlogMemberFeedbackQuery;
import com.liujit.bms.app.domain.result.BlogMemberFeedbackResult;
import com.liujit.bms.app.mapper.BlogMemberFeedbackMapper;
import com.liujit.bms.app.service.BlogMemberFeedbackService;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import com.liujit.web.utils.JwtUtil;
import org.springframework.stereotype.Service;

/**
 * @Description 博客会员反馈Service实现类
 * @Author liujun
 * @Date 2021-07-16 14:18:02
 */
@Service
public class BlogMemberFeedbackServiceImpl extends ServiceImpl<BlogMemberFeedbackMapper, BlogMemberFeedback>
        implements BlogMemberFeedbackService {

    @Override
    public PageBean<BlogMemberFeedbackResult> getPage(BlogMemberFeedbackQuery query) {
        Page<BlogMemberFeedback> page = Pagination.page(query);
        LambdaQueryWrapper<BlogMemberFeedback> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberFeedback::getMemberId, JwtUtil.getUserId());
        IPage<BlogMemberFeedback> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogMemberFeedbackResult.class);
    }

    @Override
    public Boolean save(BlogMemberFeedbackParams params) {
        BlogMemberFeedback entity = DozerConvertor.convertor(params, BlogMemberFeedback.class);
        entity.setMemberId(JwtUtil.getUserId());
        // 添加
        return super.save(entity);
    }

    @Override
    public BlogMemberFeedbackResult get(Long id) {
        BlogMemberFeedback blogMemberFeedback = super.getById(id);
        if (blogMemberFeedback == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(blogMemberFeedback, BlogMemberFeedbackResult.class);
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogMemberFeedback blogMemberFeedback = super.getById(params.getId());
        if (blogMemberFeedback == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogMemberFeedback> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberFeedback::getId, params.getId());
        wrapper.eq(BlogMemberFeedback::getMemberId, JwtUtil.getUserId());
        return super.remove(wrapper);
    }
}
